package org.vs.handler.responsibilitychain;

import com.alibaba.fastjson2.JSON;
import org.vs.domain.vo.ProgressVO;
import org.vs.service.SSEService;

import javax.annotation.Resource;
import java.util.Map;

public abstract class BaseResponsibilityChainHandler implements ResponsibilityChainHandler {
    protected ResponsibilityChainHandler handler;

    @Resource
    protected SSEService sseService;

    private String hashcode;
    private String obj;

    protected static Integer voteCount = 0;

    public BaseResponsibilityChainHandler() {
        voteCount++;
    }

    @Override
    public void handler(Object obj) {
        if (!customerHandler(obj)) {
            return;
        }
        sendMessage();
        nextHandler(obj);
    }

    protected void setHandler(ResponsibilityChainHandler handler) {
        this.handler = handler;
    }

    private void nextHandler(Object obj) {
        if (handler == null) {
            return;
        }
        handler.handler(obj);
    }

    protected void setMessage(String hashcode, String obj) {
        this.hashcode = hashcode;
        this.obj = obj;
    }

    private void sendMessage() {
        if (hashcode == null) {
            return;
        }

        sseService.sendMessage(hashcode, obj);
    }

    protected abstract boolean customerHandler(Object obj);
}
